﻿Shader "Unlit/MyUnlitShader_08"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_Distance("Distance",Float) = 1
		_ColorWidth("ColorRange",Float) = 1
		_DistancePower("Distance Power", Range(0.2 , 4)) = 1
		[HideInInspector]_Pos("Pos",Vector) = (0,0,0,0)
		_OffsetPower("Offset",Float) = 1
		[HDR]_FinalColor("Final Color", Color) = (1,0,0,1)
		[HDR]_FinalColor2("Final Color 2", Color) = (0,0,0,0)
    }
    SubShader
    {
		Tags { "Queue" = "Transparent" "RenderType" = "Opaque" }
		LOD 100
		Blend One One
		ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float y : TEXCOORD1;
				float4 color : COLOR;
            };

            sampler2D _MainTex;
			float _Distance;
			float _OffsetPower;
			float _ColorWidth;
			float _DistancePower;
			
			//声明uniform,说明_Affector需要由应用程序提供给它，如果没有提供给它，默认的是0
			uniform float4 _Affector;
			float4 _Pos;
			float4 _MainTex_ST;

			fixed4 _FinalColor;
			fixed4 _FinalColor2;

            v2f vert (appdata_full v)
            {
                v2f o;
				float4 pPos = float4(v.texcoord.z, v.texcoord.w, v.texcoord1.x, 0);
				float dis = 1 - distance(pPos, _Affector);
				float4 direct = pPos - _Affector;
				/*
                    clamp(x,a,b)    如果x值小于a，则返回a；
                    如果x值大于b，返回b；
                    否则，返回x。
				*/
				float clampResult1 = clamp(((dis + (_Distance - 1.0)) / _Distance), 0.0, 1.0);
				float clampResult2 = clamp(((dis + (_ColorWidth - 1.0)) / _ColorWidth), 0.0, 1.0);
				
				//pow(x, y): x的y次方；
				float temp_output_1 = pow(clampResult1, _DistancePower);
				float temp_output_2 = pow(clampResult2, _DistancePower);
				v.vertex.xyz += (temp_output_1 * direct * _OffsetPower).xyz;

				o.vertex = UnityObjectToClipPos(v.vertex);
				o.y = saturate(temp_output_2);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.color = v.color;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				float4 lerpResult = lerp(_FinalColor2 , _FinalColor , i.y)*i.color;
				fixed4 col = tex2D(_MainTex, i.uv);
				col *= lerpResult;
                return col;
            }
            ENDCG
        }
    }
}
